/*
 * Copyright (c) 2020 - present, Inspur Genersoft Co., Ltd.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package io.iec.edp.caf.sumgr.api.utils;

import io.iec.edp.caf.commons.utils.SpringBeanUtils;
import io.iec.edp.caf.msu.api.ServiceUnitAwareService;
import io.iec.edp.caf.sumgr.api.IServiceUnitAware;
import io.iec.edp.caf.sumgr.api.entity.MsuType;
import io.iec.edp.caf.sumgr.api.entity.ServiceUnitInfo;

import java.util.ArrayList;
import java.util.List;

@Deprecated
public class ServiceUnitAwareUtil implements IServiceUnitAware {

    //单例模式 装载时完成初始化
    private static ServiceUnitAwareUtil INSTANCE = new ServiceUnitAwareUtil();

    private static final ServiceUnitAwareService suService = SpringBeanUtils.getBean(ServiceUnitAwareService.class);

    //私有无参构造
    private ServiceUnitAwareUtil() {
    }

    //单例模式
    public static ServiceUnitAwareUtil getInstance() {
        return INSTANCE;
    }

    @Override
    public List<ServiceUnitInfo> getAllServiceUnits() {
        List<io.iec.edp.caf.msu.api.entity.ServiceUnitInfo> suInfos = suService.getAllServiceUnits();
        List<ServiceUnitInfo> result = new ArrayList<>();
        suInfos.forEach(info -> {
            ServiceUnitInfo newInfo = new ServiceUnitInfo();
            newInfo.setServiceUnitDes(info.getServiceUnitDes());
            newInfo.setApplicationName(info.getApplicationName());
            if (info.getMsuType().equals(io.iec.edp.caf.msu.api.enums.MsuType.Biz))
                newInfo.setMsuType(MsuType.Biz);
            else newInfo.setMsuType(MsuType.Common);
            newInfo.setName(info.getName());
            newInfo.setPath(info.getPath());
            newInfo.setType(info.getType());
            result.add(newInfo);
        });
        return result;
    }

    @Override
    public List<String> getEnabledServiceUnits() {
        return suService.getEnabledServiceUnits();
    }

}
